package com.czwj.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.czwj.entities.OperateResult;
import com.czwj.entities.Pressure;
import com.czwj.entities.ThirdEnterprice;
import com.czwj.realm.JwtService;
import com.czwj.service.PressureService;
import com.czwj.service.ThirdEnterpriceService;

import io.jsonwebtoken.Claims;

/**
 * 授权管理接口
 *
 * @author Like
 */

@RestController
@RequestMapping("")
@CrossOrigin(origins = "*")
public class AuthController {
	
	@Resource
	JwtService jwtService;
	@Resource
	ThirdEnterpriceService thirdEnterpriceService;
	@Resource
	PressureService pressureService;

	@PostMapping("/auth/token")
	public OperateResult login(@RequestParam("appKey") String key,
			@RequestParam("appSecret") String secret, HttpServletRequest request) {
		OperateResult result = new OperateResult();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("key", key);
			ThirdEnterprice enterprice = thirdEnterpriceService.findone(map);
			if(enterprice==null) {
				result.setCode("401");
				result.setSuccess(false);
				result.setErrMsg("appKey不存在");
				return result;
			}
			if(!enterprice.getAppsecret().equals(secret)) {
				result.setCode("402");
				result.setSuccess(false);
				result.setErrMsg("appkey和appSecret不匹配");
				return result;
			}
			String token = jwtService.createToken(enterprice.getId()+"", "");
			result.setSuccess(true);
			Map<String, Object> res = new HashMap<>();
			res.put("token", token);
			res.put("expire", 3600);
			result.setData(res);
			result.setCode("200");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setCode("500");
			result.setSuccess(false);
			result.setErrMsg("服务器错误");
		}
		return result;
	}
	
	@PostMapping("/api/getPressureData")
	public OperateResult getData(@RequestHeader("token") String token,
			@RequestParam("imei") String imei,
			@RequestParam("startTime") String starttime,
			@RequestParam("endTime") String endtime) {
		OperateResult result = new OperateResult();
		try {
			if(checkToken(token)==null) {
				result.setCode("403");
				result.setSuccess(false);
				result.setErrMsg("token失效");
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("imei", imei);
			if(StringUtils.isNotEmpty(starttime)) {
				map.put("startdate", starttime);
			}
			if(StringUtils.isNotEmpty(endtime)) {
				map.put("enddate", endtime);
			}
			List<Pressure> list = pressureService.search(map);
			result.setSuccess(true);
			result.setCode("200");
			result.setData(list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setCode("500");
			result.setSuccess(false);
			result.setErrMsg("服务器错误");
		}
		return result;
	}

	
	
	private Integer checkToken(String token) {
        Claims claims = jwtService.parseToken(token);
        if (claims != null) {
            return Integer.parseInt(claims.getId());
        }
        return null;
    }
}
